﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

public partial class payroll_payroll : System.Web.UI.Page
{
    public int payid;
    public DataSet payrollarr;
    protected void Page_Load(object sender, EventArgs e)
    {
        ScriptManager scriptManager = ScriptManager.GetCurrent(this);

        scriptManager.RegisterPostBackControl(this.CRVPAY);   
        if (!Page.IsPostBack)
        {
            // ddl_companies = utility.fillDDLCompanies(ddl_companies);
            //llenocompanias();
            if (Request.QueryString["payroll"] != null)
            {
                payid = int.Parse(Request.QueryString["payroll"].ToString());
                payrollarr = (DataSet)DB.getArrPayroll(" where id = " + payid);
              
               
                DataSet empleados = DB.getPayrollDetail(payid, "");
                if (payrollarr != null && payrollarr.Tables[0].Rows.Count > 0)
                {
                    Label2.Text = payrollarr.Tables[0].Rows[0][1].ToString();
                    lb_close.Text = payrollarr.Tables[0].Rows[0][5].ToString();
                    if (payrollarr.Tables[0].Rows[0][5].ToString().Equals("1")) { bt_edit.Visible = false; }
                    else { bt_edit.Visible = true;  }
                   
                    bt_back.Visible = true;
                    gv_general.Enabled = false;
                    //bt_delete.Visible = true;

                   
                    enableControls(false);
                    if (empleados != null)
                    {

                        gv_general.DataSource = empleados;
                        gv_igss.DataSource = empleados;
                        gv_sev.DataSource = empleados;
                        gv_general.DataBind();
                        gv_igss.DataBind();
                        gv_sev.DataBind();
                        addSuma();
                    }
                }
            }
        }
        else
        {
            /*
            if (Request.QueryString["payroll"] != null)
            {
                payid = int.Parse(Request.QueryString["payroll"].ToString());
                payrollarr = (DataSet)DB.getArrPayroll(" where id = " + payid);
                DataSet empleados = DB.getPayrollDetail(payid);
                gv_general.DataSource = empleados;
                gv_general.DataBind();
            }
            */
            if (Session["report"] != null) {
                CRVPAY.ReportSource= Session["report"];
                 
            }

        }

    }

    private void addSuma()
    {


        int cont = 1;
        Decimal Decigsst = 0;
        Decimal Decigssp = 0;
        Decimal Decigsstt = 0;
        foreach (GridViewRow ROW in gv_igss.Rows) {
            if (ROW.Visible == true)
            {
                Label num = (Label)ROW.FindControl("lbnum0");
                if (num != null) num.Text = cont.ToString();
                cont++;
                //sumo
                Label ligsst = (Label)ROW.FindControl("igsst4");
                Label ligssp = (Label)ROW.FindControl("igssp0");
                Label ligsstt = (Label)ROW.FindControl("igsstt0");
                Decigsst += Decimal.Parse(ligsst.Text);
                Decigssp += Decimal.Parse(ligssp.Text);
                Decigsstt += Decimal.Parse(ligsstt.Text);

            }
          
        }
        cont = 1;
        foreach (GridViewRow ROW in gv_sev.Rows)
        {
            if (ROW.Visible == true)
            {
                Label num = (Label)ROW.FindControl("lbnum1");
                if (num != null) num.Text = cont.ToString();
                cont++;
             
            }
        }

    }

    protected void Page_init() { 
        
    
    }
   
    protected void gv_general_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        

           
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
         
                Label lb = (Label)e.Row.Cells[1].FindControl("lbClient");
                if (lb != null && lb.Text.ToUpper().Equals("TOTALS"))
                {
                    TextBox tb1 = (TextBox)e.Row.FindControl("dayworks");
                    TextBox tb2 = (TextBox)e.Row.FindControl("eh");
                    TextBox tb8 = (TextBox)e.Row.FindControl("ehh");
                    TextBox tb3 = (TextBox)e.Row.FindControl("eo");
                    TextBox tb4 = (TextBox)e.Row.FindControl("ceo");
                    TextBox tb5 = (TextBox)e.Row.FindControl("adjustmentpos");
                    TextBox tb6 = (TextBox)e.Row.FindControl("adjustmentneg");
                    TextBox tb7 = (TextBox)e.Row.FindControl("parking");
                    if (tb1 != null) tb1.Visible = false; 
                    if (tb2 != null) tb2.Visible = false;
                    if (tb3 != null) tb3.Visible = false;
                    if (tb4 != null) tb4.Visible = false;
                    if (tb5 != null) tb5.Visible = false;
                    if (tb6 != null) tb6.Visible = false;
                    if (tb7 != null) tb7.Visible = false;
                    if (tb8 != null) tb8.Visible = false;
                }

            
            DropDownList dp = (DropDownList)e.Row.Cells[2].FindControl("ddl_igss");
            dp = (DropDownList)e.Row.Cells[1].FindControl("ddl_igss");
            DropDownList dp2 = (DropDownList)e.Row.Cells[3].FindControl("ddl_sev");
            /* dp = (DropDownList)e.Row.Cells[4].FindControl("ddl_igss");
             dp = (DropDownList)e.Row.Cells[5].FindControl("ddl_igss");*/
            if (dp != null)
            {
                if (dp.SelectedIndex == 0)
                {
                    dp.ForeColor = System.Drawing.Color.Red;
                    if (lb != null&& !lb.Text.Equals("Totals")) { lb.Text = "*" + lb.Text; }
                }

            }
            if (dp2 != null)
            {
                if (dp2.SelectedIndex == 0)
                {
                    dp2.ForeColor = System.Drawing.Color.Red;
                    if (lb != null && !lb.Text.Equals("Totals")) { lb.Text = "*" + lb.Text; }
                }

            }
            Label tb = (Label)e.Row.Cells[1].FindControl("lbClient");
            if (tb != null && tb.Text.ToUpper().Equals("TOTALS"))
            {
                DropDownList dd = (DropDownList)e.Row.Cells[2].FindControl("ddl_igss");
                dd.Visible = false;
                DropDownList dd2 = (DropDownList)e.Row.Cells[3].FindControl("ddl_sev");
                dd2.Visible = false;


            }
            foreach (TableCell cell in e.Row.Cells)
            {
                

                e.Row.Cells[5].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[6].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[7].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[8].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[9].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[10].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[11].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[12].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[13].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[14].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[15].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[16].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[17].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[18].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[19].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[20].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[21].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[22].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[23].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[24].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[25].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[26].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[27].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[28].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[29].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[30].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[31].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[32].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[33].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[34].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[35].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[36].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[37].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[38].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[39].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[40].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[41].Attributes.Add("Style", " text-align: right;");
         






            }

            
        }


    }
    
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
       // Response.Redirect("payroll_general.aspx");
        Response.Write("<script>window.close();</script>");
    }
    protected void LinkButton7_Click(object sender, EventArgs e)
    {
        if (!lb_close.Text.Equals("1"))
        {
            enableControls(true);
            bt_edit.Visible = false;
            bt_save.Visible = true;
            bt_close.Visible = true;
           
           
        }

    }

    private void enableControls(bool p)
    {
       
       // ddl_completed.Enabled = p;
        gv_general.Enabled = p;
        // ddl_account.Enabled = p;
        // ddl_company.Enabled = p;


    }
    protected void bt_save_Click(object sender, EventArgs e)
    {
        /*RE_GenericBean re = new RE_GenericBean();
        re.strC1 = tb_name.Text;
        re.strC2 = tb_startdate.Text;
        re.strC3 = tb_enddate.Text;
        re.intC1 = int.Parse(lb_id.Text);
        re.intC2 = int.Parse(ddl_completed.SelectedValue);
        int res = DB.insertPayroll(re, true);
        if (res != 0)
        {
            enableControls(false);
            bt_edit.Visible = true;
            bt_save.Visible = false;
            
            
        }
        else
        {
            Response.Write("<alert text='Error Payroll not saved!'");
        }
         * */

        save_detail();
    }

    private void save_detail()
    {
        ArrayList relist = new ArrayList();
        payid = 0;
        if (Request.QueryString["payroll"] != null)
        {
            payid = int.Parse(Request.QueryString["payroll"].ToString());
        }
        if (payid != 0)
        {
            foreach (GridViewRow ROW in gv_general.Rows)
            {


                Label employeeid = (Label)ROW.FindControl("lbid");
                if (employeeid!= null &&   employeeid.Text != "")
                {
                    TextBox daysworked = (TextBox)ROW.FindControl("dayworks");
                    TextBox extrahour = (TextBox)ROW.FindControl("eh");
                    TextBox extrahour2 = (TextBox)ROW.FindControl("ehh");
                    TextBox extraorder = (TextBox)ROW.FindControl("eo");
                    TextBox adjustmentpos = (TextBox)ROW.FindControl("adjustmentpos");
                    TextBox adjustmentneg = (TextBox)ROW.FindControl("adjustmentneg");
                    DropDownList igss = (DropDownList)ROW.FindControl("ddl_igss");
                    DropDownList severance = (DropDownList)ROW.FindControl("ddl_sev");
                    DropDownList bank = (DropDownList)ROW.FindControl("ddl_method");
                    TextBox parking = (TextBox)ROW.FindControl("parking");
                    TextBox tbcomment = (TextBox)ROW.FindControl("tbcomment");
                    RE_GenericBean re = new RE_GenericBean();
                    re.intC1 = payid;
                    re.intC2 = int.Parse(employeeid.Text);
                    if (!daysworked.Text.Equals("")) re.decC2 = Decimal.Parse(daysworked.Text);
                    if (!extrahour.Text.Equals("")) re.decC3 = Decimal.Parse(extrahour.Text);
                    if (!extrahour2.Text.Equals("")) re.decC13 = Decimal.Parse(extrahour2.Text);
                    if (!extraorder.Text.Equals("")) re.decC4 = Decimal.Parse(extraorder.Text);
                    if (!adjustmentpos.Text.Equals("")) re.decC5 = Decimal.Parse(adjustmentpos.Text);
                    if (!adjustmentneg.Text.Equals("")) re.decC6 = Decimal.Parse(adjustmentneg.Text);
                    re.decC7 = Decimal.Parse(igss.SelectedValue);
                    re.decC8 = Decimal.Parse(severance.SelectedValue);
                    re.intC6 = int.Parse(bank.SelectedValue);
                    if (!parking.Text.Equals("")) re.decC9 = Decimal.Parse(parking.Text);
                    if (!tbcomment.Text.Equals("")) re.strC1 = tbcomment.Text;

                    relist.Add(re);
                }

            }
            int res = DB.updatePayrollDetail(relist);
            DataSet empleados = DB.getPayrollDetail(payid, "");
            if (empleados != null)
            {
                gv_general.DataSource = empleados;
                gv_igss.DataSource = empleados;
                gv_sev.DataSource = empleados;
                gv_general.DataBind();
                gv_igss.DataBind();
                gv_sev.DataBind();
                addSuma();
            }

        }
    }

    public string getColor(string flag)
    {

        string result = "Black";

        if (flag.Equals("0")) result = "Red";

        return result;

    }
    protected void button_Click(object sender, EventArgs e)
    {

        payid = 0;
        if (Request.QueryString["payroll"] != null)
        {
            payid = int.Parse(Request.QueryString["payroll"].ToString());
        }
        if (payid != 0)
        {

            ImageButton bt = (ImageButton)sender;
            GridViewRow ROW = (GridViewRow)bt.Parent.Parent;

            Label employeeid = (Label)ROW.FindControl("lbid");
            if (employeeid.Text != "")
            {
                TextBox daysworked = (TextBox)ROW.FindControl("dayworks");
                TextBox extrahour = (TextBox)ROW.FindControl("eh");
                TextBox extraorder = (TextBox)ROW.FindControl("eo");
                TextBox adjustmentpos = (TextBox)ROW.FindControl("adjustmentpos");
                TextBox adjustmentneg = (TextBox)ROW.FindControl("adjustmentneg");
                DropDownList igss = (DropDownList)ROW.FindControl("ddl_igss");
                DropDownList severance = (DropDownList)ROW.FindControl("ddl_sev");
                TextBox parking = (TextBox)ROW.FindControl("parking");

                RE_GenericBean re = new RE_GenericBean();
                ArrayList relist = new ArrayList();
                re.intC1 = payid;
                re.intC2 = int.Parse(employeeid.Text);
                re.decC2 = Decimal.Parse(daysworked.Text);
                re.decC3 = Decimal.Parse(extrahour.Text);
                re.decC4 = Decimal.Parse(extraorder.Text);
                re.decC5 = Decimal.Parse(adjustmentpos.Text);
                re.decC6 = Decimal.Parse(adjustmentneg.Text);
                re.decC7 = Decimal.Parse(igss.SelectedValue);
                re.decC8 = Decimal.Parse(severance.SelectedValue);
                re.decC9 = Decimal.Parse(parking.Text);

                relist.Add(re);


                int res = DB.updatePayrollDetail(relist);
                DataSet empleados = DB.getPayrollDetail(payid, "");
                if (empleados != null)
                {

                    gv_general.DataSource = empleados;
                    gv_igss.DataSource = empleados;
                    gv_sev.DataSource = empleados;
                    gv_general.DataBind();
                    gv_igss.DataBind();
                    gv_sev.DataBind();
                    addSuma();
                }
            }
        }
    }
    protected void gv_general_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        if (sender != null)
        {

            GridView gv = (GridView)sender;
            gv.PageIndex = e.NewPageIndex;
            gv.DataBind();
        }
    }
    protected void gv_general_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
   
    protected void bt_generate_Click(object sender, EventArgs e)
    {

    }
    
    


   
   

    protected void change ( object  sender ,EventArgs e){
        GridViewRow objFila;
        try
        {

             TextBox tbsender = (TextBox)sender;
             objFila = (GridViewRow)tbsender.Parent.Parent;
        }
        catch (Exception ex) {
             DropDownList tbsender = (DropDownList)sender;
             objFila = (GridViewRow)tbsender.Parent.Parent;
        }
        
       
       TextBox tb = (TextBox)objFila.FindControl("dayworks");
       Label tdr = (Label)objFila.FindControl("dailyrate");
       Label tt = (Label)objFila.FindControl("salary");
       Label lt1 = (Label)objFila.FindControl("lbt1");
       TextBox thorasext = (TextBox)objFila.FindControl("eh");
       TextBox thorasext2 = (TextBox)objFila.FindControl("ehh");
       Label ceh = (Label)objFila.FindControl("ceh");//costo de horaextra
       Label payeh = (Label)objFila.FindControl("payeh");//pago de horaextras
       TextBox ordenes = (TextBox)objFila.FindControl("eo");
       TextBox ceo = (TextBox)objFila.FindControl("ceo");//costo de ordenextra
       Label payeo = (Label)objFila.FindControl("payeo");//pago de ordenextra
       TextBox tadjmas = (TextBox)objFila.FindControl("adjustmentpos");
       TextBox tadjme = (TextBox)objFila.FindControl("adjustmentneg");
       TextBox tparking = (TextBox)objFila.FindControl("parking");
       Label lsubmas = (Label)objFila.FindControl("t2");
       Label lsubme = (Label)objFila.FindControl("subtminus");
       DropDownList ddigss = (DropDownList)objFila.FindControl("ddl_igss");
       DropDownList ddsev = (DropDownList)objFila.FindControl("ddl_sev");
        

       Decimal days = 0;
       if (tb != null) days = Decimal.Parse(tb.Text);
       Decimal salary = 0;
        if (tt!= null) salary=  Decimal.Parse(tt.Text);
        Decimal dw = 0;
        if (tdr != null) dw= Decimal.Parse(tdr.Text);
        Decimal t1 =  days * (salary / 15);
        if (lt1!= null)  lt1.Text = Decimal.Round(t1,2).ToString();

        Decimal payhours = 0;
        Decimal costoeh = 0;
        Decimal horasex = 0;
        Decimal horasex2 = 0;
        if (thorasext != null) horasex = Decimal.Parse(thorasext.Text);
        if (thorasext2 != null) horasex2 = Decimal.Parse(thorasext2.Text);
        if (ceh != null) costoeh = Decimal.Parse(ceh.Text);
        Decimal hh = horasex2 * Decimal.Parse("1.5");
        payhours = (horasex+hh) * costoeh;
        if (payeh != null) payeh.Text = Decimal.Round(payhours, 2).ToString();

        Decimal payorders = 0;
        Decimal costooh = 0;
        Decimal ordersex = 0;
        if (ordenes != null) ordersex = Decimal.Parse(ordenes.Text);
        if (ceo != null) costooh = Decimal.Parse(ceo.Text);
        payorders = ordersex * costooh;
        if (payorders != null) payeo.Text = Decimal.Round(payorders, 2).ToString();
        Decimal t2 = 0;
        Decimal adjusmas = 0;
        if (tadjmas != null) adjusmas = Decimal.Parse(tadjmas.Text);
        if (lsubmas != null) {
            t2 = t1 + payhours + payorders+ adjusmas;
            lsubmas.Text = Decimal.Round(t2, 2).ToString();
        }

        //igss
        Decimal igsst=0;
        Decimal igssf=0;
         Decimal igsstt=0;
         Label ligss = (Label)objFila.FindControl("igsst");
        Label ligsst = (Label)objFila.FindControl("igsst2");
        Label ligsstf = (Label)objFila.FindControl("ifssf");
        Label ligsstt = (Label)objFila.FindControl("igsstt");
        if (ddigss.SelectedValue.Equals("1")){
            if (ligsstf != null) {
                igssf = Decimal.Parse(ligsstf.Text);
                igsst = igssf *  Decimal.Parse("0.0483");
                igsstt = igsst + (igssf * Decimal.Parse("0.1267"));
                ligss.Text = Decimal.Round(igsst,2).ToString();
                ligsst.Text = ligss.Text;
            }
            if (ligsstt != null) ligsstt.Text = Decimal.Round(igsstt, 2).ToString();
        }else{
            ligsstt.Text = "0.00";
            ligss.Text = "0.00";
            ligsst.Text = "0.00";
        }
        //sev
        Decimal sevbase = 0;
        Decimal sevbonus = 0;
        Decimal totalbonus = 0;
        Decimal bono13 = 0;
        Decimal agree = 0;
        Decimal sev=0;
        Label lsevbase = (Label)objFila.FindControl("sevbase");
        Label lsevbaseBONUS = (Label)objFila.FindControl("sevbaseBONUS");
        Label ltotalbonus = (Label)objFila.FindControl("totalbonus");
        Label lbono13 = (Label)objFila.FindControl("bono13");
        Label lbono14 = (Label)objFila.FindControl("bono14");
        Label lsev = (Label)objFila.FindControl("sev");
        Label lagree = (Label)objFila.FindControl("agreement");
        if (ddsev.SelectedValue.Equals("1"))
        {
            if (lsevbase != null)
            {
                sevbase = Decimal.Parse(lsevbase.Text);
                sevbonus = Decimal.Parse(lsevbaseBONUS.Text);
                bono13 = (sevbase + sevbonus)/12;
                lbono13.Text = Decimal.Round(bono13, 2).ToString();
                lbono14.Text = Decimal.Round(bono13, 2).ToString();
                agree = Decimal.Parse(lagree.Text);
                if (agree == 0) { sev = sevbase / 12; }
                else { sev = agree / 12; }

                totalbonus = bono13 + bono13 + sev;
            
                

            }
            ltotalbonus.Text = Decimal.Round(totalbonus, 2).ToString();
        }
        else
        {

            lbono13.Text = "0.00";
            lbono14.Text = "0.00";
            lsev.Text = "0.00";
            ltotalbonus.Text = "0.00";
        }
        //subtotales
        Decimal parking = 0;
        Decimal adjme = 0;
        Decimal t3 = 0;
        Decimal t4=0;
        Decimal t5=0;
        Label check = (Label)objFila.FindControl("check");
        Label grandtotal = (Label)objFila.FindControl("grandtotal");
        if (tparking != null) parking = Decimal.Parse(tparking.Text);
        if (tadjme != null) adjme = Decimal.Parse(tadjme.Text);
        t3 = parking + adjme + igsst;
        lsubme.Text = Decimal.Round(t3,2).ToString();
        t4= t2-t3;//check amount
        t5= t4+totalbonus+igsstt;
        check.Text = Decimal.Round(t4, 2).ToString();
        grandtotal.Text= Decimal.Round(t5,2).ToString();

        objFila.ForeColor = System.Drawing.Color.FromName("#0f6c2e");

}


    protected void comments_click(object sender, EventArgs e)
    {
        ImageButton tbsender = (ImageButton)sender;
        GridViewRow objFila = (GridViewRow)tbsender.Parent.Parent;
        Label lbname = (Label)objFila.FindControl("lbClient");
        Label lbidemployee = (Label)objFila.FindControl("lbid");
        Label lbpayrollid = (Label)objFila.FindControl("lbClient");
        TextBox tbcomment = (TextBox)objFila.FindControl("tbcomment");
        //tbcomment.Visible = true;
        lb_namemen.Text = lbname.Text;
        TextBox3.Text = tbcomment.Text;
        lb_employeeid.Text = lbidemployee.Text;
        if (Request.QueryString["payroll"] != null)
        {
            payid = int.Parse(Request.QueryString["payroll"].ToString());
        }
        
        lb_payrollid.Text = payid.ToString();
        Panel1.Visible = true;     
        //Panel_Add_Extender.Show();
       

        
    }


    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        //Panel_Add_Extender.Show();
    }
    protected void save_comment_Click(object sender, EventArgs e)
    {
        RE_GenericBean r = new RE_GenericBean();
        r.intC1 = int.Parse(lb_payrollid.Text);
        r.intC2 = int.Parse(lb_employeeid.Text);
        r.strC1 = TextBox3.Text;
        DB.updateCommentPayrollDetail(r);
        payid = 0;
        if (Request.QueryString["payroll"] != null)
        {
            payid = int.Parse(Request.QueryString["payroll"].ToString());
        }
        if (payid != 0)
        {
            DataSet empleados = DB.getPayrollDetail(payid, "");
            if (empleados != null)
            {
                gv_general.DataSource = empleados;
                gv_igss.DataSource = empleados;
                gv_sev.DataSource = empleados;
                gv_general.DataBind();
                gv_igss.DataBind();
                gv_sev.DataBind();
                addSuma();
            }
        }
        Panel_Add_Extender.Hide();
        Panel1.Visible = false;
    }

    protected void LinkButton3_Click1(object sender, EventArgs e)
    {
        Panel_Add_Extender.Hide();
        Panel1.Visible = false;
    }
    protected void bt_report_Click(object sender, EventArgs e)
    {
        type_extender.Show();
    }
    protected void LinkButton4_Click(object sender, EventArgs e)
    {
        if (RadioButtonList1.SelectedIndex != null && RadioButtonList1.SelectedIndex!= -1 )
        {

            payid = 0;
            if (Request.QueryString["payroll"] != null)
            {
                payid = int.Parse(Request.QueryString["payroll"].ToString());
            }
            if (payid != 0)
            {
                ds_payroll empleados = DB.getPayrollDetailDS(payid, "");
                CrystalDecisions.CrystalReports.Engine.ReportDocument rpt = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
                CRVPAY.DisplayGroupTree = false;
                rpt.FileName = "Payroll_"+Label2.Text;
                if (empleados != null)
                {
                    if (RadioButtonList1.SelectedValue == "1")
                    {
                        //Internal Report
                        
                        rpt.Load(Server.MapPath("Payroll_report_all.rpt"));
                        rpt.SetDataSource(empleados);
                        
                        Parameter nombre = new Parameter();
                        nombre.Name = "@Nombre";
                        nombre.DefaultValue = Label2.Text;
               
                       
                        rpt.ParameterFields["@Nombre"].CurrentValues.Clear();
                     
                        rpt.ParameterFields["@Nombre"].CurrentValues.AddValue(nombre.DefaultValue);
                        Session["report"] = rpt;
                                           
                        
                    }
                    else if (RadioButtonList1.SelectedValue == "2")
                    { 
                        //External Report
                        
                        rpt.Load(Server.MapPath("Payroll_report2.rpt"));
                        rpt.SetDataSource(empleados);
                        Session["report"] = empleados;
                        Parameter nombre = new Parameter();
                        nombre.Name = "@Nombre";
                        nombre.DefaultValue = Label2.Text;
                       

                        rpt.ParameterFields["@Nombre"].CurrentValues.Clear();

                        rpt.ParameterFields["@Nombre"].CurrentValues.AddValue(nombre.DefaultValue);
                        
                        Session["report"] = rpt;
                       
                    }
                    else if(RadioButtonList1.SelectedValue == "3")
                    {  //IGSS report
                        rpt.Load(Server.MapPath("Payroll_igss.rpt"));
                        rpt.SetDataSource(empleados);
                        Session["report"] = empleados;
                        Parameter nombre = new Parameter();
                        nombre.Name = "@Nombre";
                        nombre.DefaultValue = Label2.Text;


                        rpt.ParameterFields["@Nombre"].CurrentValues.Clear();

                        rpt.ParameterFields["@Nombre"].CurrentValues.AddValue(nombre.DefaultValue);

                        Session["report"] = rpt;
                    }
                    else if (RadioButtonList1.SelectedValue == "4")
                    { //Severance Report
                        rpt.Load(Server.MapPath("Payroll_sev.rpt"));
                        rpt.SetDataSource(empleados);
                        Session["report"] = empleados;
                        Parameter nombre = new Parameter();
                        nombre.Name = "@Nombre";
                        nombre.DefaultValue = Label2.Text;


                        rpt.ParameterFields["@Nombre"].CurrentValues.Clear();

                        rpt.ParameterFields["@Nombre"].CurrentValues.AddValue(nombre.DefaultValue);

                        Session["report"] = rpt;
                    
                    }
                    TabConteiner1.Visible = false;
                    bt_search.Visible = true;
                    bt_report.Visible = false;
                    P_crystal.Visible = true;
                    rpt.PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.Landscape;
                    rpt.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.PaperLegal;
                    CRVPAY.ReportSource = rpt;
                    CRVPAY.DataBind();
                }

            }
        }
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        type_extender.Hide();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (RadioButtonList1.SelectedIndex != null && RadioButtonList1.SelectedIndex != -1)
        {

            payid = 0;
            if (Request.QueryString["payroll"] != null)
            {
                payid = int.Parse(Request.QueryString["payroll"].ToString());
            }
            if (payid != 0)
            {
                ds_payroll empleados = DB.getPayrollDetailDS(payid, "");
                CrystalDecisions.CrystalReports.Engine.ReportDocument rpt = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
                CRVPAY.DisplayGroupTree = false;
                if (empleados != null)
                {
                    if (RadioButtonList1.SelectedIndex == 0)
                    {
                        //Internal Report

                        rpt.Load(Server.MapPath("Payroll_report.rpt"));
                        rpt.SetDataSource(empleados);

                        Parameter nombre = new Parameter();
                        nombre.Name = "@Nombre";
                        nombre.DefaultValue = Label2.Text;


                        rpt.ParameterFields["@Nombre"].CurrentValues.Clear();

                        rpt.ParameterFields["@Nombre"].CurrentValues.AddValue(nombre.DefaultValue);
                        Session["report"] = rpt;
                    }
                    else
                    {
                        //External Report

                        rpt.Load(Server.MapPath("Payroll_report2.rpt"));
                        rpt.SetDataSource(empleados);
                        Session["report"] = empleados;
                        Parameter nombre = new Parameter();
                        nombre.Name = "@Nombre";
                        nombre.DefaultValue = Label2.Text;


                        rpt.ParameterFields["@Nombre"].CurrentValues.Clear();

                        rpt.ParameterFields["@Nombre"].CurrentValues.AddValue(nombre.DefaultValue);
                        Session["report"] = rpt;
                    }
                   
                    CRVPAY.ReportSource = rpt;
                    CRVPAY.DataBind();

                }

            }
        }
    }
    protected void bt_search_Click(object sender, EventArgs e)
    {
        TabConteiner1.Visible = true;
        P_crystal.Visible = false;
        bt_search.Visible = false;
        bt_report.Visible = true;
    }

    protected void gv_igss_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
        {
            foreach (TableCell cell in e.Row.Cells)
            {
                //cell.Attributes.Add("Style", " border:1px solid lightblue;");


                //cell.Attributes.Add("Style", " background-color:#ffffff;");
                e.Row.Cells[0].Attributes.Add("Style", " text-align: left;");
                e.Row.Cells[1].Attributes.Add("Style", " text-align: left;");
                e.Row.Cells[2].Attributes.Add("Style", " text-align: left;");
                e.Row.Cells[3].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[4].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[5].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[6].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[7].Attributes.Add("Style", " text-align: right;");

            }
            e.Row.Cells[0].Attributes.Add("Style", " text-align: left;");
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            Label lb = (Label)e.Row.Cells[1].FindControl("lbClient0");
            if (lb != null && lb.Text.ToUpper().Equals("TOTALS"))
            {
                TextBox tb1 = (TextBox)e.Row.FindControl("dayworks");
                TextBox tb2 = (TextBox)e.Row.FindControl("eh");
                TextBox tb3 = (TextBox)e.Row.FindControl("eo");
                TextBox tb4 = (TextBox)e.Row.FindControl("ceo");
                TextBox tb5 = (TextBox)e.Row.FindControl("adjustmentpos");
                TextBox tb6 = (TextBox)e.Row.FindControl("adjustmentneg");
                TextBox tb7 = (TextBox)e.Row.FindControl("parking");
                Label lb2 = (Label)e.Row.Cells[1].FindControl("lbnum0");
                if (tb1 != null) tb1.Visible = false;
                if (tb2 != null) tb2.Visible = false;
                if (tb3 != null) tb3.Visible = false;
                if (tb4 != null) tb4.Visible = false;
                if (tb5 != null) tb5.Visible = false;
                if (tb6 != null) tb6.Visible = false;
                if (tb7 != null) tb7.Visible = false;
                if (lb2 != null) lb2.Visible = false;
            }

           
            DropDownList dp = (DropDownList)e.Row.Cells[4].FindControl("ddl_igss0");
            
            DropDownList dp2 = (DropDownList)e.Row.Cells[3].FindControl("ddl_sev");
          
            if (dp != null)
            {
                if (dp.SelectedIndex == 0)
                {
                    dp.ForeColor = System.Drawing.Color.Red;
                    e.Row.Visible = false;
                   
                }

            }
            if (dp2 != null)
            {
                if (dp2.SelectedIndex == 0) dp2.ForeColor = System.Drawing.Color.Red;

            }
            Label tb = (Label)e.Row.Cells[1].FindControl("lbClient0");
            if (tb != null && tb.Text.ToUpper().Equals("TOTALS"))
            {
                DropDownList dd = (DropDownList)e.Row.Cells[2].FindControl("ddl_igss0");
                dd.Visible = false;
               
                e.Row.Visible = true;


            }
        }
    }
    protected void gv_sev_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
        {
            foreach (TableCell cell in e.Row.Cells)
            {
                //cell.Attributes.Add("Style", " border:1px solid lightblue;");


                //cell.Attributes.Add("Style", " background-color:#ffffff;");
                e.Row.Cells[0].Attributes.Add("Style", " text-align: left;");
                e.Row.Cells[1].Attributes.Add("Style", " text-align: left;");
                e.Row.Cells[2].Attributes.Add("Style", " text-align: left;");
                e.Row.Cells[3].Attributes.Add("Style", " text-align: left;");
                e.Row.Cells[4].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[5].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[6].Attributes.Add("Style", " text-align: right;");
                e.Row.Cells[7].Attributes.Add("Style", " text-align: right;");
                // e.Row.Cells[].Text
            }
        }


        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            Label lb = (Label)e.Row.Cells[1].FindControl("lbClient1");
            if (lb != null && lb.Text.ToUpper().Equals("TOTALS"))
            {
                Label lb2 = (Label)e.Row.Cells[1].FindControl("lbnum1");
                TextBox tb1 = (TextBox)e.Row.FindControl("dayworks");
                TextBox tb2 = (TextBox)e.Row.FindControl("eh");
                TextBox tb3 = (TextBox)e.Row.FindControl("eo");
                TextBox tb4 = (TextBox)e.Row.FindControl("ceo");
                TextBox tb5 = (TextBox)e.Row.FindControl("adjustmentpos");
                TextBox tb6 = (TextBox)e.Row.FindControl("adjustmentneg");
                TextBox tb7 = (TextBox)e.Row.FindControl("parking");
                if (tb1 != null) tb1.Visible = false;
                if (tb2 != null) tb2.Visible = false;
                if (tb3 != null) tb3.Visible = false;
                if (tb4 != null) tb4.Visible = false;
                if (tb5 != null) tb5.Visible = false;
                if (tb6 != null) tb6.Visible = false;
                if (tb7 != null) tb7.Visible = false;
                if (lb2 != null) lb2.Visible = false;
            }

           
            DropDownList dp = (DropDownList)e.Row.Cells[4].FindControl("ddl_igss0");

            DropDownList dp2 = (DropDownList)e.Row.Cells[3].FindControl("ddl_sev0");
            /* dp = (DropDownList)e.Row.Cells[4].FindControl("ddl_igss");
             dp = (DropDownList)e.Row.Cells[5].FindControl("ddl_igss");*/
            
            if (dp2 != null)
            {
                if (dp2.SelectedIndex == 0)
                {
                    dp2.ForeColor = System.Drawing.Color.Red;
                    e.Row.Visible = false;
                }

            }
            Label tb = (Label)e.Row.Cells[1].FindControl("lbClient1");
            if (tb != null && tb.Text.ToUpper().Equals("TOTALS"))
            {
               
                DropDownList dd2 = (DropDownList)e.Row.Cells[3].FindControl("ddl_sev0");
                dd2.Visible = false;
                e.Row.Visible = true;

            }
        }
    }
    protected void bt_close_Click(object sender, EventArgs e)
    {
        payid=0;
        if (Request.QueryString["payroll"] != null)
        {
                payid = int.Parse(Request.QueryString["payroll"].ToString());
        }
        int res = 0;
        if (payid != 0) res= DB.closePayroll(1,payid);
        if (res == 1)
        {
            gv_general.Enabled = false;
            bt_close.Visible = false;
            bt_edit.Visible = false;
            bt_save.Visible = false;
        }

    }

    public DataTable getbancos()
    {
        ArrayList companyarr = null;
        DataTable dt = null;

        try
        {
            companyarr = (ArrayList)DB.getArrBancos("");
            if (companyarr != null && companyarr.Count > 0)
            {
                dt = utility.getDataTableBancos(companyarr);
            }
        }
        catch (Exception err)
        {
            utility.log(err.StackTrace);
        }
        return dt;
    }
}